package dev.android.yeqh.tvannounce.net;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;

import dev.android.yeqh.tvannounce.common.Globes;

public class AccessWS {
	public static String callWS(String url, List<NameValuePair> params) throws ParserConfigurationException, ClientProtocolException, IOException {
		HttpPost request = new HttpPost(url);
		
		if (params != null) {
			request.setEntity(new UrlEncodedFormEntity(params));
		}
		
		HttpResponse httpResponse = Globes.client.execute(request);
		String result = "";
		if (httpResponse.getStatusLine().getStatusCode() != 404) {
			result = EntityUtils.toString(httpResponse.getEntity());
		}
		
		return result;
	}
	
	public static String getPage(String url) throws ParserConfigurationException, ClientProtocolException, IOException {
		HttpGet request = new HttpGet(url);
		request.setHeader("Content-Type", "text/html");
		request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		
		HttpResponse httpResponse = Globes.client.execute(request);
		String result = "";
		if (httpResponse.getStatusLine().getStatusCode() < 404) {
			result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
		}
		
		return result;
	}

}
